// pages/historyjuan/historyjuan.js  
Page({  
  data: {  
    radioQuestions: [],          // 存储单选题列表  
    shortAnswerQuestions: [],    // 存储简答题列表  
    judgeQuestions: [],          // 存储判断题列表  
    examId: null,                // 用于存储试卷ID  
    currentType: null,           // 当前展示的题目类型  
    title: '题目列表'             // 答题卡标题  
  },  

  onLoad(options) {   
   // console.log('接收到的参数:', options);  
    this.examId = options.examId; // 获取试卷 ID   
    this.fetchPaperDetails(this.examId); // 修改为 this.examId  
  },  

  onShow() {  
    // 页面每次显示时请求数据  
    if (this.examId) { // 只有在 examId 有值时才请求  
      this.fetchPaperDetails(this.examId);  
    }  
  },  

  fetchPaperDetails(id) {  
    const that = this;
    const app = getApp();
    wx.request({  
      method: 'POST',  
      url: `${app.globalData.baseUrl}/paper/paper-detail`,   
      header: {  
        'authentication': 'eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MzI1NTg3MTcsInVzZXJJZCI6MTgyNzk5OTgyMTczMTA5ODYyNn0.SxS7ENOxgqgIh8AAn_Sw84R_zvFA99_khzsVNCDShLk'  
      },  
      data: {  
        id: id // 使用传入的试卷ID  
      },  
      success(res) {  
        //console.log('请求成功:', res.data)  
        if (res.statusCode === 200) {  
          const { code, data } = res.data;  
          if (code === 1 && data) {  
            // 提取三个题目列表  
            const { radioList = [], shotAnswerList = [], judgeList = [] } = data;  

            // 更新题目列表  
            that.setData({  
                radioQuestions: radioList,  
                shortAnswerQuestions: shotAnswerList,  
                judgeQuestions: judgeList  
            })

            // 打印获取到的题目列表  
           // console.log('单选题列表:', that.data.radioQuestions);  
            //console.log('简答题列表:', that.data.shortAnswerQuestions);  
            //console.log('判断题列表:', that.data.judgeQuestions); 
            
            // 根据题目类型设置当前显示的题目  
            if (radioList.length > 0) {  
              that.setData({ currentType: 'radio', title: '单选题' });  
            } else if (shotAnswerList.length > 0) {  
              that.setData({ currentType: 'shortAnswer', title: '简答题' });  
            } else if (judgeList.length > 0) {  
              that.setData({ currentType: 'judge', title: '判断题' });  
            } else {  
              that.setData({ currentType: null, title: '暂无题目' });  
            }  
            // 将题目列表缓存到本地  
           wx.setStorageSync(`radiopapers`, that.data.radioQuestions);
           wx.setStorageSync(`shotAnswerpapers`, that.data.shortAnswerQuestions);
          // console.log('简答题',that.data.shotAnswerList);
           wx.setStorageSync(`judgepapers`, that.data.judgeQuestions);

          } else {  
            console.error('获取试卷详情失败，数据格式不正确:', res.data);  
          }  
        } else {  
          console.error('请求失败:', res);  
        }  
      },  
      fail(err) {  
        console.error('请求失败:', err);  
      }  
    });  
  },   

  // 点击题目时的事件  
  onQuestionClick(event) {  
    const index = event.currentTarget.dataset.index; // 获取题目下标  
    const quType = event.currentTarget.dataset.qutype; // 获取 quType  
    let paperId; // 声明 paperId  
    let quId; // 声明 quId  

    //console.log('下标', index);  

    // 根据当前题目类型获取对应题目信息  
    if (quType === '1') {  
        paperId = this.data.radioQuestions[index]?.paperId;  
        quId = this.data.radioQuestions[index]?.quId;  
    } else if (quType === '3') {  
        paperId = this.data.shortAnswerQuestions[index]?.paperId;  
        quId = this.data.shortAnswerQuestions[index]?.quId;   
    } else if (quType === '2') {  
        paperId = this.data.judgeQuestions[index]?.paperId;  
        quId = this.data.judgeQuestions[index]?.quId;  
    }  

    // 打印调试信息  
    //console.log('当前题目类型:', this.data.currentType);  
    //console.log('选择的题目ID:', quId, '对应题目类型:', quType);  

    // 检查 paperId 和 quId 是否成功获取到值  
    if (paperId && quId) {  
        // 跳转到 chudan 页面，并传递 paperId 和 quId  
        wx.navigateTo({  
            url: `/pages/lishida/lishida?paperId=${paperId}&quId=${quId}&index=${index}&quType=${quType}`  
        });  

      //  console.log('paperId:', paperId, 'quId:', quId, 'quType:', quType);  
    } else {  
        console.error('未能获取有效的 paperId 或 quId');  
    }  
},

  onGoackhome() {   
    wx.showModal({  
      title: '提示',  
      content: '是否要结束答题',  
      success: (res) => {   
        if (res.confirm) {  
          wx.switchTab({  
            url: `/pages/home/home`  
          });  
          console.log('用户点击确定');  
        } else if (res.cancel) {  
          console.log('用户点击取消');  
        }   
      },  
    });  
  },  
});